<?php
namespace app\common\server;
use app\MyController;

class RedisLock extends MyController{
    private $_redis;

    public function initialize(){
        
        $this->_redis = new \Redis();
        $this->_redis->connect('127.0.0.1',6379);
    }

    /**
     * 获取锁
     * @param String $key  锁标识
     * @param Int   $expire 锁过期时间
     * @return Boolean
     */
    public function lock($key, $expire=5){
        $is_lock = $this->_redis->setnx($key, time()+$expire);
        // 不能获取锁
        if(!$is_lock){
            // 判断锁是否过期
            $lock_time = $this->_redis->get($key);
            // 锁已过期，删除锁，重新获取
            if(time()>$lock_time){
                $this->unlock($key);
                $is_lock = $this->_redis->setnx($key, time()+$expire);
            }
        }

        return $is_lock? true : false;
    }


    /**
     * 释放锁
     * @param String $key 锁标识
     * @return Boolean
     */
    public function unlock($key){
        return $this->_redis->del($key);
    }



}